package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwbcT;
import cn.e4j.modular.crm.entity.GwbclT;
import cn.e4j.modular.crm.mapper.GwbcTMapper;
import cn.e4j.modular.crm.service.GwbcTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  关键项评估 服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-04-12
 */
@Service
public class GwbcTServiceImpl extends ServiceImpl<GwbcTMapper,GwbcT> implements GwbcTService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {

        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);

    }

    @Transactional
    @Override
    public void add(GwbcT gwbcT, GwbclT gwbclT) {

        ShiroUser shiroUser = ShiroKit.getUser();
        gwbcT.setGWBCENT(shiroUser.getDBC());

        gwbcT.setGWBC003(" ");
        gwbcT.setGWBC004(" ");
        gwbcT.setGWBC005(" ");
        gwbcT.setGWBCDATE(LocalDate.now());
        gwbcT.setGWBCTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwbcT.setGWBCUSER(shiroUser.getACCOUNT());
        gwbcT.setGWBCGRUP(shiroUser.getORG_ID());
        gwbcT.setGWBCORIU(shiroUser.getACCOUNT());
        gwbcT.setGWBCORIG(shiroUser.getORG_ID());

        gwbclT.setGWBCLENT(shiroUser.getDBC());
        gwbclT.setGWBCL001(gwbcT.getGWBC001());
        gwbclT.setGWBCL002(LangUtils.getLang(shiroUser.getLANG()));

        gwbcT.insert();
        gwbclT.insert();
    }

    @Transactional
    @Override
    public void edit(GwbcT gwbcT, GwbclT gwbclT) {

        ShiroUser shiroUser= ShiroKit.getUser();
        gwbcT.setGWBCENT(shiroUser.getACCOUNT());

        UpdateWrapper<GwbcT> gwbcTUpdateWrapper = new UpdateWrapper<>();
        gwbcTUpdateWrapper.eq("GWBCENT", gwbcT.getGWBCENT());
        gwbcTUpdateWrapper.eq("GWBC001", gwbcT.getGWBC001());

        UpdateWrapper<GwbclT> gwbclTUpdateWrapper = new UpdateWrapper<>();
        gwbclTUpdateWrapper.eq("GWBCLENT", gwbcT.getGWBCENT());
        gwbclTUpdateWrapper.eq("GWBCL001", gwbcT.getGWBC001());


        gwbcT.update(gwbcTUpdateWrapper);
        gwbclT.update(gwbclTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRow(GwbcT gwbcT, GwbclT gwbclT) {

        UpdateWrapper<GwbcT> gwbcTUpdateWrapper = new UpdateWrapper<>();
        gwbcTUpdateWrapper.eq("GWBCENT", gwbcT.getGWBCENT());
        gwbcTUpdateWrapper.eq("GWBC001", gwbcT.getGWBC001());

        UpdateWrapper<GwbclT> gwbclTUpdateWrapper = new UpdateWrapper<>();
        gwbclTUpdateWrapper.eq("GWBCLENT", gwbcT.getGWBCENT());
        gwbclTUpdateWrapper.eq("GWBCL001", gwbcT.getGWBC001());


        gwbcT.delete(gwbcTUpdateWrapper);
        gwbclT.delete(gwbclTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRows(List<GwbcT> gwbcTList) {
        for (GwbcT gwbcT : gwbcTList) {
            UpdateWrapper<GwbcT> gwbcTUpdateWrapper = new UpdateWrapper<>();
            gwbcTUpdateWrapper.eq("GWBCENT", gwbcT.getGWBCENT());
            gwbcTUpdateWrapper.eq("GWBC001", gwbcT.getGWBC001());

            GwbclT gwbclT=new GwbclT();
            UpdateWrapper<GwbclT> gwbclTUpdateWrapper = new UpdateWrapper<>();
            gwbclTUpdateWrapper.eq("GWBCLENT", gwbcT.getGWBCENT());
            gwbclTUpdateWrapper.eq("GWBCL001", gwbcT.getGWBCENT());

            gwbcT.delete(gwbcTUpdateWrapper);
            gwbclT.delete(gwbclTUpdateWrapper);
        }
    }

    /**
     * 查重
     * @param gwbcT
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(GwbcT gwbcT) {
        QueryWrapper<GwbcT> gwbcTQueryWrapper = new QueryWrapper<>();
        gwbcTQueryWrapper.eq("GWBCENT", gwbcT.getGWBCENT());
        gwbcTQueryWrapper.eq("GWBC001", gwbcT.getGWBC001());
        return this.baseMapper.selectList(gwbcTQueryWrapper).size() > 0;
    }
}
